<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/26
 * Time: 16:19
 */

namespace Application\Models;

use BlogServices\ArticleServiceIf;
use Phalcon\Mvc\Model;

class Article extends Model implements ArticleServiceIf
{
    public function initialize()
    {
        $this->setSource("learn_article");
    }

    /**
     * @param string $where
     * @param int $offset
     * @param int $limit
     * @return \BlogServices\Article[]
     */
    public function GetArticles($where, $offset, $limit)
    {
        $record = $this->find()->toArray();
        $arr = [];
        foreach ($record as $item) {
            $article = new \BlogServices\Article();
            $article->Id = intval($item['id']);
            $article->Title = $item['title'];
            $article->Keywords = $item['keywords'];
            $article->Description = $item['description'];
            $article->Imgurl = $item['imgurl'];
            $article->Content = $item['content'];
            $article->Sort = intval($item['sort']);
            $article->CatId = intval($item['cat_id']);
            $article->ViewNum = intval($item['viewnum']);
            $article->Url = $item['url'];
            $article->CreateAt = intval($item['create_at']);
            $article->UpdateAt = intval($item['update_at']);
            $article->Markdown = $item['markdown'];
            $arr[] = $article;
        }
        return $arr;
    }

    /**
     * @param int $catid
     * @param int $offset
     * @param int $limit
     * @return \BlogServices\Article[]
     */
    public function GetArticlesByCatId($catid, $offset, $limit)
    {
        return [];
    }

    /**
     * @param int $id
     * @return \BlogServices\Article
     */
    public function GetArticleById($id)
    {
        $item = (array) self::findFirst($id);
        $article = new \BlogServices\Article();
        $article->Id = intval($item['id']);
        $article->Title = $item['title'];
        $article->Keywords = $item['keywords'];
        $article->Description = $item['description'];
        $article->Imgurl = $item['imgurl'];
        $article->Content = $item['content'];
        $article->Sort = intval($item['sort']);
        $article->CatId = intval($item['cat_id']);
        $article->ViewNum = intval($item['viewnum']);
        $article->Url = $item['url'];
        $article->CreateAt = intval($item['create_at']);
        $article->UpdateAt = intval($item['update_at']);
        $article->Markdown = $item['markdown'];
        $arr[] = $article;
        return $article;
    }

    /**
     * @param string $where
     * @return int
     */
    public function GetArticleTotal($where)
    {
        // TODO: Implement GetArticleTotal() method.
    }
}